package com.zmsk.zmio.handler;

import android.content.Context;
import android.util.Log;

import com.zmsk.zmio.inter.IIOHandler;
import com.zmsk.zmio.ysface88.YSGpioUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class XJHHandler implements IIOHandler {

    private static final String TAG = "YSLM8Handler";

    public XJHHandler() {

    }

    @Override
    public void initIOControl(Context context) {

    }

    @Override
    public void stopIOControl(Context context) {

    }

    @Override
    public void noticeGateOpen(Context context, String idNumber) {
        ctlLedRelay("9");

    }

    @Override
    public void noticeGateClose(Context context) {
        ctlLedRelay("10");
    }

    @Override
    public void noticeWhiteLedOn(Context context) {
        ctlLedRelay("5");
    }

    @Override
    public void noticeWhiteLedOff(Context context) {
        ctlLedRelay("6");
    }

    @Override
    public void noticeInfraredLedOn(Context context) {
        ctlLedRelay("7");
    }

    @Override
    public void noticeInfraredLedOff(Context context) {
        ctlLedRelay("8");
    }

    @Override
    public void noticeRedLedOn(Context context) {

    }

    @Override
    public void noticeRedLedOff(Context context) {

    }

    @Override
    public void noticeGreenLedOn(Context context) {

    }

    @Override
    public void noticeGreenLedOff(Context context) {

    }

    @Override
    public void handleSlientInstall(String path) {

    }

    private static void ctlLedRelay(String val) {
        File file = new File("/sys/class/zh_gpio_out/out");
        if (!file.exists() || !file.canWrite()) {
            Log.e("Utils","LED ctl path is not exists or can not write!!");
            return; }
        try {
            FileOutputStream fout = new FileOutputStream(file); PrintWriter pWriter = new PrintWriter(fout); pWriter.println(val);
            pWriter.flush();
            pWriter.close();
            fout.close();
        } catch (IOException re) {
            Log.e("Utils", "write error:" + re);
        }
    }

}
